package com.baidu.unlockdemo.lifecyle

import android.content.Context
import android.media.MediaPlayer
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.baidu.unlockdemo.R

/**
 * 一个类想要接收Activity或者Fragment的生命周期
 * 必须 继承于 DefaultLifecycleObserver
 *
 * 宿主：activity fragment
 */
class MusicPlayer(context: Context): DefaultLifecycleObserver {
    private val player: MediaPlayer by lazy {
        MediaPlayer.create(context, R.raw.ruoyuan)
    }

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        //创建播放器对象
    }

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        //player.stop()
    }

    override fun onPause(owner: LifecycleOwner) {
        super.onPause(owner)
        player.pause()
    }

    //welcome  mainActivity
    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        player.start()
    }

    override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)

    }

    override fun onStop(owner: LifecycleOwner) {
        super.onStop(owner)
    }

    //welecome   mainActivity
    //返回
}